package com.digitalxpert.completer;
import java.awt.event.FocusEvent;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import javax.swing.JFormattedTextField;
import javax.swing.JFormattedTextField.AbstractFormatter;
import javax.swing.text.DefaultFormatter;

public class SelectableFormattedField extends JFormattedTextField{
    boolean inFocus;
    public SelectableFormattedField(NumberFormat format){
        super(format);
    }
    public SelectableFormattedField(DefaultFormatter formatter){
        super(formatter);
    }
    public SelectableFormattedField(SimpleDateFormat df){
        super(df);
    }
    protected void processFocusEvent(FocusEvent e){
        super.processFocusEvent(e);
        if(e.getID() == FocusEvent.FOCUS_GAINED){
            selectAll();
        }
        if(e.getID() == FocusEvent.FOCUS_LOST){
            try{
                AbstractFormatter formatter = getFormatter();
                if(formatter != null){
                    formatter.stringToValue(this.getText());
                }
            }catch (ParseException e1){
                e1.printStackTrace();
            }
        }
    }
}
